package com.mayisoftware.mayioa.business.plan.impl.controller;

import com.mayisoftware.mayioa.business.plan.api.domain.*;
import com.mayisoftware.mayioa.business.plan.api.service.*;
import com.mayisoftware.mayioa.common.api.annotation.Log;
import com.mayisoftware.mayioa.common.api.base.AjaxResult;
import com.mayisoftware.mayioa.common.api.enums.BusinessType;
import com.mayisoftware.mayioa.platform.impl.util.ShiroUtils;
import com.mayisoftware.mayioa.platform.impl.web.base.BaseController;
import com.mayisoftware.mayioa.platform.impl.web.page.TableDataInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 计划配置
 */
@Controller
@RequestMapping("/plan/config/detail")
public class PlanConfigDetailController extends BaseController{
    private String prefix = "plan/config/detail";

    @Autowired
    private IPlanConfigService planConfigService;

    @Autowired
    private IPlanConfigDetailService planDetailService;

    @Autowired
    private IPlanClassService planClassService;

    @Autowired
    private IPlanCustomService planCusService;

    @Autowired
    private IPlanArchService planArchService;

    /**
     * 查看计划项目详情
     */
    @RequiresPermissions("plan:config:view")
    @GetMapping()
    public String planConfigDetail()
    {
        return prefix + "/detail";
    }

    /**
     * 新增计划项目
     */
    @GetMapping("/add/{planCfgId}")
    public String add(@PathVariable("planCfgId") Long planCfgId, ModelMap mmap)
    {
        mmap.put("planCfgId", planCfgId);
        return prefix + "/add";
    }

    /**
     * 新增保存计划子项目
     */
    @RequiresPermissions("plan:config:add")
    @Log(title = "计划子项目管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult addSave(PlanDetailConfig planItem)
    {
        planItem.setPlanCfgItmeUpdateUser(ShiroUtils.getUserId());
        return toAjax(planDetailService.insertPlanItem(planItem));
    }

    /**
     * 修改计划项目
     */
    @GetMapping("/edit/{planCfgItemRecordid}")
    public String edit(@PathVariable("planCfgItemRecordid") Long planCfgItemRecordid, ModelMap mmap)
    {
        PlanDetailConfig detail = planDetailService.selectPlanItemById(planCfgItemRecordid);
        mmap.put("planConfigDetail",detail);

        List<Map<String, Object>> itemOption = new ArrayList<>();
        Map<String, Object> dataMap = new HashMap<>();
        dataMap = new HashMap<>();
        dataMap.put("itemId", detail.getPlanCfgItemId());
        dataMap.put("itemName", detail.getPlanCfgItemName());
        itemOption.add(dataMap);

        String param = detail.getPlanCfgItemPlanId() + "#" + detail.getPlanCfgItemType();
        mmap.put("itemOption",itemOption.addAll(this.selectPlanItemByType(param)));

        return prefix + "/edit";
    }

    /**
     * 修改保存计划子项目
     */
    @RequiresPermissions("plan:config:edit")
    @Log(title = "计划子项目管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult editSave(PlanDetailConfig planItem)
    {
        planItem.setPlanCfgItmeUpdateUser(ShiroUtils.getUserId());
        return toAjax(planDetailService.updatePlanItem(planItem));
    }

    @RequiresPermissions("plan:config:remove")
    @Log(title = "计划子项目管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(planDetailService.deletePlanItemByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 校验计划子项目名称唯一
     */
    @PostMapping("/checkPlanItemNameUnique")
    @ResponseBody
    public String checkPlanItemNameUnique(PlanDetailConfig planItem)
    {
        return planDetailService.checkPlanItemNameUnique(planItem.getPlanCfgItemName());
    }

    @PostMapping("/checkPublish")
    @ResponseBody
    public AjaxResult checkPublish(String id)
    {
        try
        {
            int num = planConfigService.checkPlanCfgIsPublish(Long.valueOf(id));
            // 已发布
            if(num > 0)
            {
                return success("不能修改已发布的计划");
            }
            return error();
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }

    }

    /**
     * 查询计划子项目列表
     */
    @RequiresPermissions("plan:config:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PlanDetailConfig planItem)
    {
        startPage();
        List<PlanDetailConfig> list = planDetailService.selectPlanItemList(planItem);
        return getDataTable(list);
    }

    /**
     * 查询不属于当前计划的课程
     * @param param
     * @return
     */
    @GetMapping("/selectPlanItemByType")
    @ResponseBody
    public List<Map<String, Object>> selectPlanItemByType(String param)
    {
        Long planCfgId = Long.valueOf(param.split("#")[0]);
        // 1-课程，2-自定义，3-绩效
        Long planCfgItemType = Long.valueOf(param.split("#")[1]);

        List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
        Map<String, Object> dataMap = null;
        // 1-课程
        if(planCfgItemType == 1)
        {
            List<PlanClass> list = planClassService.selectPlanClassList(planCfgId);
            if(CollectionUtils.isNotEmpty(list))
            {
                for (PlanClass cls : list)
                {
                    dataMap = new HashMap<>();
                    dataMap.put("itemId", cls.getPlanClassId());
                    dataMap.put("itemName", cls.getPlanClassName());
                    dataList.add(dataMap);
                }
            }
        }
        // 2-自定义
        else if(planCfgItemType == 2)
        {
            List<PlanCustom> list = planCusService.selectPlanCusList(planCfgId);
            if(CollectionUtils.isNotEmpty(list))
            {
                for (PlanCustom custom : list)
                {
                    dataMap = new HashMap<>();
                    dataMap.put("itemId", custom.getPlanCusId());
                    dataMap.put("itemName", custom.getPlanCusName());
                    dataList.add(dataMap);
                }
            }
        }
        // 3-绩效
        else if(planCfgItemType == 3)
        {
            List<PlanAchievement> list = planArchService.selectPlanAchList(planCfgId);
            if(CollectionUtils.isNotEmpty(list))
            {
                for (PlanAchievement achievement : list)
                {
                    dataMap = new HashMap<>();
                    dataMap.put("itemId", achievement.getPlanAchId());
                    dataMap.put("itemName", achievement.getPlanAchName());
                    dataList.add(dataMap);
                }
            }
        }

        return dataList;
    }

}
